Ext.namespace('Ext.ux.grid');

/**
 * @class Ext.ux.grid.CollectGrid
 * @version: 1.0
 * @author: zhaixm
 *
 * Example : Ext.onReady(function(){	
		      var columnDescribe = [
		      {
		         dataIndex:"", //the ColumnModel options alse the JsonReader mapping (required)
		         header:"",    //the ColumnModel options (required) 
		         visiable: false, //my expands option to show the header or not (options)
		         type: date,   //the type of this data (options)
		         ...another options of ColumnModel
		      },{   
		         dataIndex : '',   
		         header : "",   
		         width : 130   
		      }]; 
		     
		      var myGrid = new Ext.ux.grid.CompSimpleList({
		      	  id:'',
			      dataUrl : 'MyJsp.jsp',          // the store load url (required)
			      columnDescribe: columnDescribe, //.....(required)
			      rowNumber:true,             //true to add a Ext.grid.RowNumberer,defalut(true)
			      checkBox:true,              //true to add a Ext.grid.CheckBoxSelectionModel,default(true)
			      pagingBar:true,             //true to add a Ext.PagingToolBar,default(true)
			      pagingConfig:objcet,        //config pagingToolBar if pagingBar is true {pageSize:pageSize, rowCount:rowCount}
			      qryParams:object  // 
			      width : 700, 
			      height: 600, 
			      title : 'This is My Grid'  ,
			      renderTo: 'my_grid'
			  });
			  myGrid.store.load({params:{start:0,limit:myGrid.pagingConfig.pageSize}});
			  //myUxGrid.render();
		      myGrid.on('rowclick',function(grid,rowIndex,e){
		      	alert(grid.getStore().getAt(rowIndex).data['stat_id']);
		      }
		      );
		 }
		 );
 */
Ext.ux.grid.CompSimpleList = Ext.extend(Ext.grid.GridPanel,{
	
	/*
	 * set your id Column Name
	 * @default : this.columnDescribe[0].dataIndex
	 */
	idColName:'',
	
	/*
	 * set this.store.load url;
	 * @type: string
	 */
	dataUrl: '',
	
	/*
	 * show the rowNumber or not
	 * @type: boolean
	 * @default: true
	 */
	rowNumber : true,
	
	/*
	 * set the grid sm,if checkBoxSelection=true,sm=CheckBoxSelectionModel
	 * else sm=RowSelectionModel,default to true;
	 * @type: boolean
	 */
	checkBox: true,
	
	/**
	 * 列描述
	 * set the grid cm array;
	 * set the JsonReader record;
	 * 
	 * format: [{dataIndex:'',header:'',visiable:'',...another cm options},{}],
	 * @name=dataIndex
	 * @visiable: set this record to the cm(grid header) default(true)
	 * @type: array (records)
	 */	
	columnDescribe: null,
	
	/*
	 * true to add a bottom paging bar
	 * @defalut: true
	 * @type: boolean
	 */
	pagingBar: true,
	
	/*
	 * config paging bar if pagingBar set true
	 * @type: object
	 * @default: {pageSize: 20,store: this.store,displayInfo: true,
	 *            displayMsg: '当前记录数: {0} - {1} 总记录数: {2}',
	 *            emptyMsg: '<b>0</b> 条记录'}
	 */
	pagingConfig: null,
	
	qryParams:{},
	
	viewConfig:{
		forceFit: true
	},
	
	initComponent: function(){
		if(this.columnDescribe){
			this.initCMAndStore();
		}
		if(this.pagingBar){
			this.initPagingBar();
		}
		Ext.ux.grid.CompSimpleList.superclass.initComponent.call(this);
	},
	
	/**
	 * 初始化选择器模型、列模型和Store
	 * @return: void
	 * @params: none
	 */
	initCMAndStore: function(){
		var gCm = new Array();
		var gRecord = new Array();
		if(this.rowNumber){
			gCm[gCm.length]=new Ext.grid.RowNumberer();
		}
		if(this.checkBox){
			var sm = new Ext.grid.CheckboxSelectionModel();
			gCm[gCm.length] = sm;
			this.selModel = sm;
		}
		for(var i=0;i<this.columnDescribe.length;i++){
			var g = this.columnDescribe[i];
			if(g.visiable || g.visiable=='undefined' || g.visiable==null){
				gCm[gCm.length] = g;
			}
			gRecord[gRecord.length]={
				name: g.dataIndex,
				type: g.type || 'string' 
			};
		}
	
		//create grid columnModel
		this.cm = new Ext.grid.ColumnModel(gCm);
		this.cm.defaultSortable = true;
		
		//create a jsonStore
		this.store = new Ext.data.Store({
			proxy: new Ext.data.HttpProxy({
				url: this.dataUrl,
				method: 'post'
			}),
			reader:new Ext.data.JsonReader({
				totalProperty: 'totalCount',
				root: 'records'
			},
			Ext.data.Record.create(gRecord))
		});
	},
	
	/**
	 * 创建并初始化paging bar
	 */
	initPagingBar: function(){
		var defaultPagingConfig = {
			start: 0,
			pageSize: 10,
			displayInfo: true,
		    displayMsg: '当前记录数: {0} - {1} 总记录数: {2}',
		    emptyMsg: '<b>0</b> 条记录'
		};
		// 创建GridPanel的底部工具栏
		Ext.apply(this.pagingConfig, defaultPagingConfig);
		if(!this.pagingConfig.rowCount) {
			this.pagingConfig.rowCount = -1;
		}
		this.pagingConfig.store = this.store;
		this.bbar = new Ext.PagingToolbar(this.pagingConfig);
		// 设置GridPanel的查询参数
		Ext.apply(this.qryParams, this.pagingConfig);
		if(this.pagingBar){
			this.store.load({params:this.qryParams});
		}else{
			this.store.load();
		}
	}
});

Ext.ux.grid.QrySchemeSimpleList = Ext.extend(Ext.ux.grid.CompSimpleList, {
	/**
	 * 获取列表配置信息的url地址
	 * @type String
	 */
	confUrl:'',
	initComponent: function(){
		Ext.Ajax.request({
			url: this.confUrl,
			method: 'get',
			success: function(response) {
				var rtnInfo = Ext.util.JSON.decode(response.responseText);
				var listFields = rtnInfo.queryScheme.listFields;
				var cols = new Array();
				for(var i=0; i<listFields.length; i++){
					var col = new Object();
					col.header = listFields[i].entityField.nameCn;
					col.dataIndex = listFields[i].entityField.name;
					col.sortable = true;
					if(!listFields[i].display){
						col.hidden = true;
					}
					cols[cols.length] = col;
				}
				this.columnDescribe = cols;
				var page = rtnInfo.page;
				this.pagingConfig = {pageSize:page.pageSize, rowCount:page.rowCount};
				Ext.ux.grid.QrySchemeSimpleList.superclass.initComponent.call(this);
			},
			failure: function(form,action){
				Ext.Msg.alert("提示","获取列表配置信息失败");
			}
		});
	}
});